﻿using UnityEngine;
using DragonBones;
using System.Collections;
using System;

public class PlayerController : MonoBehaviour
{
    UnityArmatureComponent ArmatureControll = null;

    public KeyCode attack = KeyCode.J;

    bool running = false;
    Rigidbody2D rigidbody;
    public float XSpeed = 10, YSpeed = 4;

    //执行的动画
    public string ArmatureMode = Armature.STAND;

    //跑步
    public bool isRunning;

    //走路
    public bool isWalking;

    Coroutine timer;

    //攻击、吟唱、施法等不允许移动的行动
    public bool isWorking;

    // Use this for initialization
    void Start()
    {
        ArmatureControll = gameObject.GetComponent<UnityArmatureComponent>();
        ArmatureControll.animation.Play("stand");
        rigidbody = GetComponent<Rigidbody2D>();
    }

    void Update()
    {
        float h = Input.GetAxis("Horizontal");
        float v = Input.GetAxis("Vertical");
        bool j = Input.GetKey(attack);

        isWalking = false;
        isRunning = false;

        if (!isWorking)
        {
            if (h != 0 || v != 0)
            {
                rigidbody.MovePosition(new Vector2(transform.position.x, transform.position.y) + new Vector2(h * XSpeed, v * YSpeed) * Time.deltaTime);
            }

            if (h > 0)
            {
                transform.localScale = new Vector3(-1, 1, 1);
            }
            if (h < 0)
            {
                transform.localScale = new Vector3(1, 1, 1);
            }

            if (v != 0 || h != 0)
            {
                isWalking = true;
            }

            if (h > 0.5 || h < -0.5)
            {
                isRunning = true;
            }

            if (j)
            {
                workArmature(Armature.ATTACK);
            }
        }
        updateArmature();
    }

    private void updateArmature()
    {
        String i = ArmatureMode;
        if (!isWorking)
        {
            i = Armature.STAND;
            if (isWalking) i = Armature.WALK;
            if (isRunning) i = Armature.RUN;
        }
        if (i != ArmatureMode)
        {
            ArmatureMode = i;
            ArmatureControll.animation.FadeIn(i,0.2f);
        }
    }

    /// <summary>
    /// 站着 播放一段动画 播放时角色不允许移动
    /// </summary>
    /// <param name="name">动画名称</param>
    public void workArmature(String name, int playTimes = 1)
    {
        isWorking = true;
        ArmatureMode = Armature.ATTACK;
        ArmatureControll.animation.FadeIn(ArmatureMode, -1.0f, playTimes);
        timer = StartCoroutine("wait", 0.6f);
    }

    public void reset()
    {
        if (timer != null)
        {
            StopCoroutine(timer);
        }
        isWalking = false;
        isRunning = false;
        isWorking = false;
        updateArmature();
    }

    IEnumerator wait(float s)
    {
        yield return new WaitForSeconds(s);
        isWorking = false;
        timer = null;
    }
}

/// <summary>
///  所有动画枚举
/// </summary>
public static class Armature
{
    public const string STAND = "stand";
    public const string RUN = "run";
    public const string WALK = "walk";
    public const string ATTACK = "attack";
}